package com.fyzk.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCLQResponseInfo;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.utils.ButtonUtils;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 混关投注adapter
 *
 * @author fxs
 */
public class JCLQHGAdapter extends JCLQBaseAdapter {

    public JCLQHGAdapter(Activity activity, List<JCLQResponseInfo> infos) {
        super(activity, infos);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final JCLQMatchData data = infos.get(groupPosition).matchDatas.get(childPosition);
        final ChildHolder childHolder;
        if (convertView == null) {
            convertView = miInflater.inflate(R.layout.jclq_adapter_child_hg, null);
            childHolder = new ChildHolder();
            childHolder.xianShi = convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            childHolder.ll_xiangqing = convertView.findViewById(R.id
                    .jc_main_list_item_yincang);
            childHolder.league_tv = convertView.findViewById(R.id.jclq_child_base_league);
            childHolder.teamId_tv = convertView.findViewById(R.id.jclq_child_base_teamid);
            childHolder.time_tv = convertView.findViewById(R.id.jclq_child_base_time);
            childHolder.home_tv = convertView.findViewById(R.id.jclq_child_sfc_home);
            childHolder.away_tv = convertView.findViewById(R.id.jclq_child_sfc_away);
            childHolder.zhanKai_check = convertView.findViewById(R.id.jclq_list_hh_zhankai);
            childHolder.cb_rsf_sheng = convertView.findViewById(R.id.jclq_list_sheng);
            childHolder.jclq_list_r_fen = convertView.findViewById(R.id.jclq_list_r_fen);
            childHolder.rang = convertView.findViewById(R.id.rang);
            childHolder.cb_rsf_fu = convertView.findViewById(R.id.jclq_list_fu);
            childHolder.cb_sf_sheng = convertView.findViewById(R.id.jclq_list_sf_sheng);
            childHolder.cb_sf_fu = convertView.findViewById(R.id.jclq_list_sf_fu);
            childHolder.stop_sf = convertView.findViewById(R.id.jclq_list_stop_sf);
            childHolder.stop_rsf = convertView.findViewById(R.id.jclq_list_stop_rsf);
            childHolder.stop_dxf = convertView.findViewById(R.id.jclq_list_stop_dxf);
            childHolder.cb_big = convertView.findViewById(R.id.jclq_list_hh_big);
            childHolder.text_fen = convertView.findViewById(R.id.jclq_list_hh_fen);
            childHolder.cb_small = convertView.findViewById(R.id.jclq_list_hh_small);
            childHolder.rsf_linear = convertView.findViewById(R.id.jclq_rf_linear);
            childHolder.sf_linear = convertView.findViewById(R.id.jclq_sf_linear);
            childHolder.dxf_linear = convertView.findViewById(R.id.jclq_dxf_linear);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        JCLQMatchData.SPInfo sf_s = data.splInfos.get(data.VS_3);
        JCLQMatchData.SPInfo sf_f = data.splInfos.get(data.VS_0);
        JCLQMatchData.SPInfo rs = data.splInfos.get(data.LETVS_3);
        JCLQMatchData.SPInfo rf = data.splInfos.get(data.LETVS_0);
        JCLQMatchData.SPInfo big = data.splInfos.get(data.DXF_1);
        JCLQMatchData.SPInfo small = data.splInfos.get(data.DXF_2);
        if (data.getStaticSaleSfStatus() == 2) {
            childHolder.cb_sf_sheng.setText(sf_s.str +"\n"+ sf_s.spValue);
            childHolder.cb_sf_fu.setText(sf_f.str+"\n"+ sf_f.spValue);
            childHolder.sf_linear.setVisibility(View.VISIBLE);
            childHolder.stop_sf.setVisibility(View.GONE);
        } else {
            childHolder.sf_linear.setVisibility(View.GONE);
            childHolder.stop_sf.setVisibility(View.VISIBLE);
        }
        if(data.getHandicap().contains("+")){
            childHolder.rang.setBackgroundColor(Color.parseColor("#FE4A4B"));
        }else {
            childHolder.rang.setBackgroundColor(Color.parseColor("#698be8"));
        }
        if (data.getStaticSaleRfsfStatus() == 2) {
            childHolder.cb_rsf_sheng.setText(rs.str.replace("让分","")+"(" + data.getHandicap()+")"+"\n" + rs.spValue);
            childHolder.cb_rsf_fu.setText(rf.str.replace("让分","") +"\n"+ rf.spValue);
            childHolder.jclq_list_r_fen.setText("主\n"+data.getHandicap());
            childHolder.rsf_linear.setVisibility(View.VISIBLE);
            childHolder.stop_rsf.setVisibility(View.GONE);
        } else {
            childHolder.rsf_linear.setVisibility(View.GONE);
            childHolder.stop_rsf.setVisibility(View.VISIBLE);
        }

        if (data.getStaticSaleDxfStatus() == 2) {
            childHolder.cb_big.setText(big.str+"("+data.getPresetScore()+")"+"\n" + big.spValue);
            childHolder.text_fen.setText(data.getPresetScore());
            childHolder.cb_small.setText(small.str +"("+data.getPresetScore()+")"+"\n"+ small.spValue);
            childHolder.dxf_linear.setVisibility(View.VISIBLE);
            childHolder.stop_dxf.setVisibility(View.GONE);
        } else {
            childHolder.dxf_linear.setVisibility(View.GONE);
            childHolder.stop_dxf.setVisibility(View.VISIBLE);
        }
        childHolder.league_tv.setText(data.getHomeTeam());
        try {
            String teamId = data.getMatchNum().substring(data.getMatchNum().length() - 3, data
                    .getMatchNum().length());
            childHolder.league_tv.setText(teamId);
            childHolder.teamId_tv.setText(data.getMatchName());
            String endData = new SimpleDateFormat("HH:mm").format(data.getEndSaleTime());
            childHolder.time_tv.setText(endData + "[截]");
        } catch (Exception e) {

        }
        childHolder.home_tv.setText(data.getHomeTeam() + "[主]");
        childHolder.away_tv.setText(data.getAwayTeam() + "[客]");
        childHolder.cb_big.setOnCheckedChangeListener(null);
        childHolder.cb_small.setOnCheckedChangeListener(null);
        childHolder.cb_rsf_sheng.setOnCheckedChangeListener(null);
        childHolder.cb_rsf_fu.setOnCheckedChangeListener(null);
        childHolder.cb_sf_sheng.setOnCheckedChangeListener(null);
        childHolder.cb_sf_fu.setOnCheckedChangeListener(null);
        childHolder.cb_rsf_sheng.setChecked(rs.isSel);
        childHolder.cb_rsf_fu.setChecked(rf.isSel);
        childHolder.cb_sf_sheng.setChecked(sf_s.isSel);
        childHolder.cb_sf_fu.setChecked(sf_f.isSel);
        childHolder.cb_big.setChecked(big.isSel);
        childHolder.cb_small.setChecked(small.isSel);
        childHolder.cb_rsf_sheng.setTag(rs);
        childHolder.cb_rsf_fu.setTag(rf);
        childHolder.cb_sf_sheng.setTag(sf_s);
        childHolder.cb_sf_fu.setTag(sf_f);
        childHolder.cb_big.setTag(big);
        childHolder.cb_small.setTag(small);
        childHolder.cb_rsf_sheng.setTag(R.string.jc_info, data);
        childHolder.cb_rsf_fu.setTag(R.string.jc_info, data);
        childHolder.cb_sf_sheng.setTag(R.string.jc_info, data);
        childHolder.cb_sf_fu.setTag(R.string.jc_info, data);
        childHolder.cb_big.setTag(R.string.jc_info, data);
        childHolder.cb_small.setTag(R.string.jc_info, data);
        childHolder.cb_big.setOnClickListener(listener);
        childHolder.cb_small.setOnClickListener(listener);
        childHolder.cb_sf_sheng.setOnClickListener(listener);
        childHolder.cb_sf_fu.setOnClickListener(listener);
        childHolder.cb_rsf_sheng.setOnClickListener(listener);
        childHolder.cb_rsf_fu.setOnClickListener(listener);
        if (data.getSfcNum() > 0) {
            childHolder.zhanKai_check.setText("胜分差\n" + data.getSfcNum());
            childHolder.zhanKai_check.setBackgroundResource(R.drawable.shape_rect_red);
            childHolder.zhanKai_check.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            childHolder.zhanKai_check.setText(R.string.jclq_moregame);
            childHolder.zhanKai_check.setBackgroundResource(R.drawable.shape_rect_ffffff);
            childHolder.zhanKai_check.setTextColor(context.getResources().getColor(R.color.color_666666));
        }
        childHolder.zhanKai_check.setTag(R.string.jc_info, data);
        childHolder.zhanKai_check.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                JCLQMatchData data = (JCLQMatchData) v.getTag(R.string.jc_info);
                if (data.hideView != null) {
                    data.hideView = null;
                    data.isAddhideView = false;
                }
                changCi = 0;
                for (int i = 0; i < infos.size(); i++) {
                    for (int j = 0; j < infos.get(i).matchDatas.size(); j++) {
                        JCLQMatchData data_item = infos.get(i).matchDatas.get(j);
                        if (data_item.clickNum > 0) {
                            changCi++;
                        }
                    }
                }
                if (data.clickNum == 0 && changCi == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                } else {
                    if (ButtonUtils.isFastDoubleClick(500)) {
                        return;
                    }
                    data.sfc_isZhanKai = true;
                    if(popOpenListener != null){
                        popOpenListener.open(data);
                    }
                }
            }
        });
        if (data.isShowHideView) {
            childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            childHolder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (data.hideView != null) {
            ViewGroup parent1 = (ViewGroup) data.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            childHolder.ll_xiangqing.removeAllViews();
            childHolder.ll_xiangqing.addView(data.hideView);
        }

        // 点击显示隐藏信息
        childHolder.xianShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (!data.isAddhideView) {
//                    childHolder.ll_xiangqing.removeAllViews();
//                    childHolder.ll_xiangqing.addView(new BasketballMatchSummaryView(context, data.getMatchNum()));
//                    data.isAddhideView = true;
//                }
//                data.isShowHideView = !data.isShowHideView;
//                if (data.isShowHideView) {
//                    childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    childHolder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", false);
                intent.putExtra("matchNum", data.getMatchNum());
                context.startActivity(intent);
            }

        });
//        childHolder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, BasketballScoreDetailActivity.class);
//                intent.putExtra("matchNum", data.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            JCLQMatchData.SPInfo spInfo = (JCLQMatchData.SPInfo) v.getTag();
            JCLQMatchData data = (JCLQMatchData) v.getTag(R.string.jc_info);
            spInfo.isSel = !spInfo.isSel;
            int clickNum = data.clickNum;
            if (spInfo.isSel) {
                data.clickNum++;
            } else {
                data.clickNum--;
            }
            if (spInfo.isSel && changCi == maxCount && data.clickNum == 1) {
                AppToast.getToast().show("最多选择" + maxCount + "场");
                spInfo.isSel = false;
                data.clickNum = clickNum;
                ((CheckBox) v).setChecked(false);
            }
            tongJi();
        }
    };


    @Override
    protected View getRightView(final JCLQMatchData data, int position) {
        return null;
    }

    class ChildHolder {
        TextView rang;
        TextView league_tv;
        TextView teamId_tv;
        TextView time_tv;

        LinearLayout rsf_linear;
        CheckBox cb_rsf_sheng;
        TextView jclq_list_r_fen;
        CheckBox cb_rsf_fu;

        CheckBox cb_big;
        TextView text_fen;
        CheckBox cb_small;

        TextView stop_sf;
        TextView stop_rsf;
        TextView stop_dxf;
        TextView home_tv;
        TextView away_tv;

        TextView zhanKai_check;
        LinearLayout sf_linear;
        LinearLayout dxf_linear;
        CheckBox cb_sf_fu;
        CheckBox cb_sf_sheng;

        LinearLayout xianShi;
        LinearLayout ll_xiangqing;
    }
}
